/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function createRequestObject(){
    var req;
    if(window.XMLHttpRequest){
        req = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        //For IE 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {

    }

    return req;
}


function sendRequest(method, url, tag_id, tipo){
    //Make the XMLHttpRequest Object
    var http = createRequestObject();

    if(!tipo)tipo=1;

    if(method == 'get' || method == 'GET'){
        http.open(method,url);
        //http.onreadystatechange = handleResponse;
        http.onreadystatechange = function(){
                                        if(http.readyState == 4 && http.status == 200){
                                            var response = http.responseText;
                                            if(response){
                                                //alert(response);
                                                var div = document.getElementById(tag_id);

                                                if(tag_id && div){
                                                    if(tipo == 1){
                                                        div.innerHTML = response;
                                                    } else if(tipo == 2){
                                                        div.innerHTML = div.innerHTML + response;
                                                    } else if(tipo == 0){
                                                        //No hace nada, solo llama al servlet pero no se muestra en pantalla
                                                    }


                                                    if(tipo != 0){
                                                        /*Para ejecutar los script que existan en la respuesta*/
                                                        ejecutarJavascriptScriptEmbebido(div, "script");
                                                        //ejecutarJavascriptScriptEmbebido(div, "SCRIPT");
                                                    }
                                                }
                                            }
                                        } else {
                                            //alert(http.readyState + " - " + http.status);
                                        }
                                    }
        http.send(null);
    }
}

function ejecutarJavascriptScriptEmbebido(div, tag){
    var elementos = div.getElementsByTagName(tag);
    //alert(elementos.length);
    for(var i=0;i<elementos.length;i++) {
        var elemento = elementos[i];
        //alert(elemento.innerHTML);
        nuevoScript = document.createElement(tag);
        nuevoScript.text = elemento.innerHTML;
        nuevoScript.type = 'text/javascript';
        if(elemento.src!=null && elemento.src.length>0){
            nuevoScript.src = elemento.src;
        }
        elemento.parentNode.replaceChild(nuevoScript,elemento);
    }
}

/*function handleResponse(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){
            document.getElementById("reg_div_short_description_place_id").innerHTML = response;
        }
    }
}*/